package com.example.synccenter.event;

import com.example.synccenter.identitysource.registry.IdentitySourceBeanRegistry;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 身份源事件处理器
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class IdentitySourceEventHandler {
    
    private final IdentitySourceBeanRegistry beanRegistry;

    /**
     * 处理配置变更事件
     */
    @EventListener
    public void handleConfigEvent(IdentitySourceConfigEvent event) {
        log.info("处理身份源配置变更事件: id={}, type={}, eventType={}", 
                event.getId(), event.getType(), event.getEventType());
                
        try {
            switch (event.getEventType()) {
                case CREATE, UPDATE, ENABLE -> beanRegistry.register(event.getId());
                case DELETE, DISABLE -> beanRegistry.destroy(event.getId());
            }
        } catch (Exception e) {
            log.error("处理身份源配置变更事件失败: {}", e.getMessage(), e);
            throw e;
        }
    }
} 